<?php
namespace app\admin\controller;

use app\common\logic\User AS UserLogic;
use app\common\logic\HelpTo AS HelpToLogic;
use app\common\component\user\UserModel;
use app\common\component\user\UserService;
use app\common\component\user\UserValidate;

class User extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

	public function index() {
		$userModel = new UserModel();

		$username = $this -> request -> get('username');

		$userModel -> order('status DESC, user_id DESC');
		if($username) {
			$userModel -> where('username', 'like' , '%' . $username . '%');
			$users = $userModel -> paginate(10, false, [
				'query' => [
						'username' => $username
					]
				]
			);
		} else {
			$users = $userModel -> paginate(10);
		}

		$this -> view -> assign('users', $users);

		return $this -> view -> fetch();
	}

	public function create() {
		if($this -> request -> isPost()) {
			$resultUser = (new UserLogic) -> register($this -> request -> post());

			if($resultUser['code'] === 0) {
				echo '<meta charset="utf-8"><script>alert("' . $resultUser['message'] . '!");window.location.href="/shuashuale/user";</script>';exit;
			}
		}

		return $this -> view -> fetch();
	}

	public function update() {
		$userService = new UserService();
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			$userId = $this -> request -> get('user_id');
			if(empty($post['password'])) {
				unset($post['password']);
			} else {
				$post['password'] = getPasswordHash($post['password']);
			}

			if(empty($post['pay_password'])) {
				unset($post['pay_password']);
			} else {
				$post['pay_password'] = getPasswordHash($post['pay_password']);
			}

			$getUser = UserModel::get(['user_id' => $userId]);

			if(($getUser['bank_code'] != $post['bank_code']) && UserModel::get(['bank_code' => $post['bank_code']])) {
				echo '<meta charset="utf-8"><script>alert("此云付通账号:' . $post['bank_code'] . '已经存在!");window.history.go(-1);</script>';exit;
			}

			if(($getUser['phone'] != $post['phone']) && UserModel::get(['phone' => $post['phone']])) {
				echo '<meta charset="utf-8"><script>alert("此手机号:' . $post['phone'] . '已经存在!");window.history.go(-1);</script>';exit;
			}

			$resultUserUpdate = $userService -> update($post, $userId);
			if($resultUserUpdate['code'] === 0) {
				echo '<meta charset="utf-8"><script>alert("' . $resultUserUpdate['message'] . '");window.location.href="/shuashuale/user";</script>';exit;
			} else {
				$this -> error('没有修改任何信息!');
			}
		}

		$resultUser = $userService -> getUser($this -> request -> get('user_id'));
		if($resultUser['data']['user'] -> promotion_id > 0 ) {			
			$resultPromotion = $userService -> getUser($resultUser['data']['user'] -> promotion_id);
			if($resultPromotion['code'] == 0) {
				$resultUser['data']['user'] -> promotion = $resultPromotion['data']['user'] -> toArray();
			} else {
				$resultUser['data']['user'] -> promotion = ['username' => '无推荐人'];
			}
		} else {
			$resultUser['data']['user'] -> promotion = ['username' => '无推荐人'];
		}

		$this -> view -> assign('user', $resultUser['data']['user']);
		return $this -> view -> fetch();
	}

	public function freeze() {
		$get = $this -> request -> get();

		switch($get['status']) {
			case 4:
				(new HelpToLogic()) -> freezeHelpTo($get['user_id']);
				break;
		}

		$resultUserUpdate = (new UserService()) -> update(['status' => $get['status']], $get['user_id']);
		if($resultUserUpdate['code'] === 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultUserUpdate['message'] . '");window.location.href="/shuashuale/user";</script>';exit;
		} else {
			$this -> error($resultUserUpdate['message']);
		}
	}

	public function into() {
		$userService = new UserService();
		$resultUser = $userService -> getUser($this -> request -> get('user_id'));

		$userService -> setLoginSession($resultUser['data']['user'] -> user_id, $resultUser['data']['user'] -> username);
		echo '<meta charset="utf-8"><script>window.location.href="/";</script>';
	}

	public function delete() {
		$resultUser = (new UserService()) -> delete($this -> request -> get('user_id'));

		if($resultUser['code'] === 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultUser['message'] . '");window.location.href="/shuashuale/user";</script>';exit;
		} else {
			$this -> error($resultUser['message']);
		}
	}

	public function promotionTree() {
		$username = $this -> request -> get('username');
		if($username) {
			$promotions = [];
			$field = 'user_id, promotion_id, username, realname, phone, createtime';
			$resultUser = UserModel::where('username', $username) -> field($field) -> find();
			$promotions[0]['text'] = $resultUser['username'];

			if($resultUser) {
				$resultUserChildren = UserModel::where('promotion_id', $resultUser['user_id']) -> field($field) -> select();

				$promotions[0]['tags'] = [count($resultUserChildren)];
				if($resultUserChildren) {
					foreach($resultUserChildren as $resultUserChild) {
						$promotions[0]['nodes'][]['text'] = $resultUserChild['username'];

						$resultUserChildrenChildren = UserModel::where('promotion_id', $resultUserChild['user_id']) -> field($field) -> select();

						$key = count($promotions[0]['nodes']) - 1;
						$promotions[0]['nodes'][$key]['tags'] = [count($resultUserChildrenChildren)];
						if($resultUserChildrenChildren) {
							foreach($resultUserChildrenChildren as $resultUserChildrenChildrenChild) {
								$promotions[0]['nodes'][$key]['nodes'][] = ['text' => $resultUserChildrenChildrenChild['username'], 'tags' => [0]];
							}
						}
					}
				}
				/*
				for($i = 1;$i <= getParam('USER_REWARD_LAYERS');$i++) {
					
				}
				*/
	    		$this -> view -> assign('promotions', json_encode($promotions));
			}
		}
		return $this -> view -> fetch();
	}
}